﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Oliving.Common.LogHelper
{


    public class OlivingLogTxt
    {
        private static object xflag = new object();
        private static FileStream createFile(string filename)
        {
            return new FileStream(filename, FileMode.Append);
        }

        //  OlivingLogTxt.writeLog(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Log_Context", Newtonsoft.Json.JsonConvert.SerializeObject(OperationDto).ToString());
        public static void writeLog(string path, string filename, string msgstr)
        {
            //return;
            lock (xflag)
            {
                path = path + "/log";
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                    //return;
                }
                string filepath = path + "/" + filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream fs = createFile(filepath);

                byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  :" + msgstr + "\r\n");
                fs.Write(data, 0, data.Length); fs.Flush();
                fs.Close();
            }
        }
        public static void writeLogCsv(string path, string filename, string msgstr)
        {
            //return;
            lock (xflag)
            {
                if (!System.IO.Directory.Exists(path))
                {
                    return;
                }
                string subpath = DateTime.Now.ToString("yyyyMM");
                string filepath = path + "/" + subpath;
                if (!System.IO.Directory.Exists(filepath))
                {
                    System.IO.Directory.CreateDirectory(filepath);
                }
                filepath = filepath + "/" + filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
                FileStream fs = createFile(filepath);

                byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "," + msgstr + "\r\n");
                fs.Write(data, 0, data.Length); fs.Flush();
                fs.Close();
            }
        }

        public static void EdgeLog(string filename, string msgstr)
        {
            writeLog(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, filename, msgstr);
        }



    }







}
